package com.wyp.design.pattern.creational.singleton.serializabledestory;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * 使用序列化和反序列化进行破坏单例模式产生出新对象分分析该问题，解决该问题
 *
 * @author: yipeng.wang01@hand-china.com-25490
 * @TIME: 2020/1/2/17:43
 **/
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        HungrySingleton instance = HungrySingleton.getInstance();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton_file"));
        oos.writeObject(instance);

        File file = new File("singleton_file");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        HungrySingleton newInstance = (HungrySingleton) ois.readObject();
        System.out.println(newInstance);
        System.out.println(instance);
        System.out.println(newInstance == instance);

    }
}
